<?php
include_once('config/config.php');

if ($_POST) {

  $action = $_POST['action'];
  if($action == 'stuname'){
    $sql = "SELECT id,name AS stuname FROM `stu_student`";
    $data = all($sql);
    if(!$data){
      echo error('学生姓名查询失败',$data,0);
    exit;
    }
    echo success('学生姓名查询成功',$data);
    exit;
  }
  if($action == 'subname'){
    $sql = "SELECT id,name AS subname FROM `stu_subject`";
    $data = all($sql);
    if(!$data){
      echo error('学科查询失败',$data,0);
    exit;
    }
    echo success('学科查询成功',$data);
    exit;
  }
  $data = [
    'stuid' => $_POST['stuname'],
    'subid' => $_POST['subname'],
    'score'=>$_POST['score'],
  ];
  $res = add('grade', $data);
  if($res)
  {
    ShowMsg('新增成绩单成功','gradelist.php');
  }else{
    ShowMsg('新增成绩单失败');
  }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php') ?>
</head>

<style>
  input[type="file"] {
    display: none;
  }

  label[for="avatar"] {
    width: 200px;
  }
</style>

<body>
  <!--<![endif]-->

  <!-- 引用头部 -->
  <?php include_once('common/header.php') ?>

  <!-- 引用菜单 -->
  <?php include_once('common/nav.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">新增成绩单</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li><a href="gradelist.php">GradeList</a> <span class="divider">/</span></li>
      <li class="active">GradeAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='gradelist.php'"><i class="icon-list"></i>成绩列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">

                <label>学生姓名</label>
                <select name="stuname" id="">
                  <option value="">请选择</option>
                </select>
                <label>学科名称</label>
                <select name="subname" id="">
                  <option value="0">请选择</option>
                </select>
                <label for="">分数</label>
                <input type="number" name="score" class="input-xxlarge" placeholder="请输入分数">

                <label></label>
                <input class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>
</body>

</html>
<?php include_once('common/script.php'); ?>
<script>
  $.ajax({
    url:'gradeadd.php',
    type:'post',
    dataType:'json',
    data:{
      action:'stuname'
    },
    success(res){
      if(res.code==1){
        let option = ''
        for (const item of res.data) {
          option += `<option value="${item.id}">${item.stuname}</option>`
        }
        $("[name='stuname']").html(option)
      }
    },
    error(err){
      console.log('err',err);
    }
  })
  $.ajax({
    url:'gradeadd.php',
    type:'post',
    dataType:'json',
    data:{
      action:'subname'
    },
    success(res){
      if(res.code==1){
        let option = ''
        for (const item of res.data) {
          option += `<option value="${item.id}">${item.subname}</option>`
        }
        $("[name='subname']").html(option)
      }
    },
    error(err){
      console.log('err',err);
    }
  })
</script>